package org.example.likou150.array;


/*
* https://leetcode.cn/problems/roman-to-integer/submissions/611155974/?envType=study-plan-v2&envId=top-interview-150
* */
public class RomanToInt {

    public int romanToInt(String s) {
        char[] charArray = s.toCharArray();
        int sum=0;
        for (int i = 0; i < charArray.length; i++) {
            int cover = cover(charArray[i]);
            if(i<=charArray.length-2){
                int next=cover(charArray[i+1]);
                if(cover<next){
                    cover=-cover;
                }
            }
            sum+=cover;
        }
        return sum;
    }
    private int cover(char c){
        switch (c){
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            default:
                throw new RuntimeException("不直到传的什么玩意");
        }
    }
}
